package cn.iocoder.yudao.module.biz.service.htPlanExe;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.htPlanExe.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.htPlanExe.HtPlanExeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.htPlanExe.BizHtPlanExeConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.htPlanExe.HtPlanExeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 排查计划执行任务 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HtPlanExeServiceImpl implements HtPlanExeService {

    @Resource
    private HtPlanExeMapper htPlanExeMapper;

    @Override
    public String createHtPlanExe(HtPlanExeCreateReqVO createReqVO) {
        // 插入
        HtPlanExeDO htPlanExe = BizHtPlanExeConvert.INSTANCE.convert(createReqVO);
        htPlanExeMapper.insert(htPlanExe);
        // 返回
        return htPlanExe.getId();
    }

    @Override
    public void updateHtPlanExe(HtPlanExeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateHtPlanExeExists(updateReqVO.getId());
        // 更新
        HtPlanExeDO updateObj = BizHtPlanExeConvert.INSTANCE.convert(updateReqVO);
        htPlanExeMapper.updateById(updateObj);
    }

    @Override
    public void deleteHtPlanExe(String id) {
        // 校验存在
        this.validateHtPlanExeExists(id);
        // 删除
        htPlanExeMapper.deleteById(id);
    }

    private void validateHtPlanExeExists(String id) {
        if (htPlanExeMapper.selectById(id) == null) {
            throw exception(HT_PLAN_EXE_NOT_EXISTS);
        }
    }

    @Override
    public HtPlanExeDO getHtPlanExe(String id) {
        return htPlanExeMapper.selectById(id);
    }

    @Override
    public List<HtPlanExeDO> getHtPlanExeList(Collection<String> ids) {
        return htPlanExeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<HtPlanExeDO> getHtPlanExePage(HtPlanExePageReqVO pageReqVO) {
        return htPlanExeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<HtPlanExeDO> getHtPlanExeList(HtPlanExeExportReqVO exportReqVO) {
        return htPlanExeMapper.selectList(exportReqVO);
    }

    @Override
    public boolean isPlanExeExist(Map<String,Object> map) {
        boolean isExist=true;
        int num=htPlanExeMapper.countPlanExe(map);
        if(num==0){
            isExist=false;
        }
        return isExist;
    }

    @Override
    public List<HtPlanExeJoinPageVO> selectHtPlanExePage(HtPlanExePageReqVO pageVO) {
        return htPlanExeMapper.selectHtPlanExePage(pageVO);
    }

    @Override
    public Integer selectHtPlanExePageCount(HtPlanExePageReqVO pageVO) {
        return htPlanExeMapper.selectHtPlanExePageCount(pageVO);
    }
}
